package com.gklyj.controller.zhaoping;

import com.gklyj.pojo.Zixun;
import com.gklyj.service.applicationService;
import com.gklyj.service.userService;
import com.gklyj.service.zixunService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.List;

@Controller
@Transactional
public class Zp_indexController {

    @Autowired
    applicationService applicationService;

    @Autowired
    userService userService;

    @Autowired
    zixunService zixunService;
    //跳转首页
    @GetMapping("/toZpWelcomePage")
    public String toZpWelcomePage(Model model, HttpServletRequest request){
        //获取当前年月日
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//定义格式，不显示时分秒
        Timestamp now= new Timestamp(System.currentTimeMillis());//获取系统当前时间
        String today= df.format(now);
        //将获取的当天时间减去一天
        Calendar calendar = Calendar.getInstance(); //创建Calendar 的实例
        calendar.add(Calendar.DAY_OF_MONTH, -1); //当前时间减去一天
        String yesterday = df.format(calendar.getTime());

        //查询当前招聘单位
        Object username = request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(String.valueOf(username));

        //获取当天 和 昨天 发布申请职位的数量
        Integer todayApplication = applicationService.GetOneDayOneDepartmentApplicationNumber(today,department);
        Integer yesterdayApplication = applicationService.GetOneDayOneDepartmentApplicationNumber(yesterday,department);

        //获取资讯今天 和 昨天 岗位申请的数量差值
        int applicationTrend = todayApplication - yesterdayApplication;

        model.addAttribute("todayApplication",todayApplication);
        model.addAttribute("applicationTrend",applicationTrend);
        //获取审核中的申请信息数量
        model.addAttribute("nowApply",applicationService.GetNowApplyApplicationNumber(department));

        return "zhaoping/zp-welcome";
    }

    //待审核申请信息数量需要单独查询，便于首页实时刷新
    @GetMapping("/getNowApply")
    @ResponseBody
    public Integer getNowApply(HttpServletRequest request){
        //查询当前招聘单位
        Object username = request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(String.valueOf(username));
        return applicationService.GetNowApplyApplicationNumber(department);
    }


    /*
    获取招聘单位资讯信息
     */
    @GetMapping("/getZpZixunList")
    @ResponseBody
    public List<Zixun> getZpZixunList(HttpServletRequest request){
        //查询当前招聘单位
        Object username = request.getSession().getAttribute("username");
        String department = userService.findDepartmentByUsername(String.valueOf(username));
        return zixunService.FindOneDepartmentZixunList(department);
    }
}
